<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="指标名称" prop="name">
                <el-input
                    v-model="queryParams.name"
                    placeholder=""
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item label="考核方向" prop="purpose">
                <el-input
                    v-model="queryParams.purpose"
                    placeholder=""
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >查询</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['hrm:item:add']"
                        >新增</el-button
                    >
                </el-col>
            </el-row>

            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="itemList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="index" label="序号" />
                <el-table-column label="指标名称" align="center" prop="name" />
                <el-table-column label="指标描述" align="center" prop="desc" />
                <el-table-column
                    label="考核方向"
                    align="center"
                    prop="purpose"
                />
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['hrm:item:edit']"
                            >修改</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['hrm:item:remove']"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 10"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>

        <!-- 添加或修改指标管理对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="500px"
            append-to-body
        >
            <TableCard>
                <el-form
                    ref="form"
                    :model="form"
                    :rules="rules"
                    label-width="120px"
                >
                    <el-form-item label="指标名称" prop="name">
                        <el-input
                            v-model="form.name"
                            placeholder="请输入指标名称"
                        />
                    </el-form-item>
                    <el-form-item
                        label="指标描述"
                        prop="desc"
                        :rules="
                            $store.getters.allDefectObj['HR-WEB-139'] == 0
                                ? [
                                      {
                                          required: true,
                                          message: '指标描述不能为空',
                                          trigger: 'blur',
                                      },
                                      {
                                          max: 50,
                                          message:
                                              '指标描述长度不能超过50个字符',
                                          trigger: 'blur',
                                      },
                                      //限制只能是数字
                                      {
                                          pattern: /^[0-9]*$/,
                                          message: '只能输入数字',
                                          trigger: 'blur',
                                      },
                                  ]
                                : [
                                      {
                                          required: true,
                                          message: '指标描述不能为空',
                                          trigger: 'blur',
                                      },
                                      {
                                          max: 50,
                                          message:
                                              '指标描述长度不能超过50个字符',
                                          trigger: 'blur',
                                      },
                                  ]
                        "
                    >
                        <el-input
                            type="textarea"
                            rows="5"
                            v-model="form.desc"
                            placeholder="请输入指标描述"
                        />
                    </el-form-item>
                    <el-form-item label="考核方向" prop="purpose">
                        <el-input
                            v-model="form.purpose"
                            placeholder="请输入考核方向"
                        />
                    </el-form-item>
                </el-form>
            </TableCard>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">提 交</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listItem,
    getItem,
    delItem,
    addItem,
    updateItem,
} from "@/api/hrm/item";

export default {
    name: "Item",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 指标管理表格数据
            itemList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                // isAsc: "descending",
                name: null,
                desc: null,
                purpose: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    {
                        required: true,
                        message: "指标名称不能为空",
                        trigger: "blur",
                    },
                    {
                        max: 20,
                        message: "指标名称长度不能超过20个字符",
                        trigger: "blur",
                    },
                ],
                desc: [
                    {
                        required: true,
                        message: "指标描述不能为空",
                        trigger: "blur",
                    },
                    {
                        max: 50,
                        message: "指标描述长度不能超过50个字符",
                        trigger: "blur",
                    },
                ],
                purpose: [
                    {
                        required: true,
                        message: "考核方向不能为空",
                        trigger: "blur",
                    },
                    {
                        max: 20,
                        message: "考核方向长度不能超过20个字符",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询指标管理列表 */
        getList() {
            this.loading = true;
            listItem(this.queryParams).then((response) => {
                this.itemList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                name: null,
                desc: null,
                purpose: null,
                createTime: null,
                createBy: null,
                updateBy: null,
                updateTime: null,
            };
            this.resetForm("form");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "新增加指标";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getItem(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "修改指标";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateItem(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            if (
                                this.$store.getters.allDefectObj[
                                    "HR-WEB-137"
                                ] == 0
                            ) {
                                this.resetQuery();
                            }
                            this.getList();
                        });
                    } else {
                        addItem(this.form).then((response) => {
                            if (
                                this.$store.getters.allDefectObj[
                                    "HR-WEB-137"
                                ] == 0
                            ) {
                                this.resetQuery();
                            }
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },

        delItemFuc(ids){
            const that = this ; 
            delItem(ids).then(()=>{
                if (that.$store.getters.allDefectObj[
                                    "HR-WEB-137"
                                ] == 0
                            ) {
                                that.resetQuery();
                            }
                            else{
                                that.getList();
                            }
                            that.$modal.msgSuccess("已经删除完成！");
                            
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            const that = this; 
            this.$modal
                .confirm(
                    "指标删除后不可恢复，且有关联业务发生的指标，不能删除。确认删除？"
                )
                .then( ()=> {
  
                               
                            that.delItemFuc(ids).then(()=>{
                              
                        
        
                })} ).catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "hrm/item/export",
                {
                    ...this.queryParams,
                },
                `item_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
